﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadData();
            trangthai_an();
        }
        Control.Product pro = new Control.Product();
        Control.Suppliers sup = new Control.Suppliers();
        Control.Categories cat = new Control.Categories();
        void LoadData()
        {
            dataGridView1.DataSource = pro.Select();
            cmbSupplier.DataSource = sup.Select();
            cmbSupplier.ValueMember = "SupplierID";
            cmbSupplier.DisplayMember = "CompanyName";
            cmbCategory.DataSource = cat.Select();
            cmbCategory.ValueMember = "CategoryID";
            cmbCategory.DisplayMember = "CategoryName";
            cmbSupplier.SelectedIndex = -1;
            cmbCategory.SelectedIndex = -1;
        }
        
        void trangthai_hien()
        {
            textBox2.ReadOnly = false;
            textBox1.ReadOnly = false;
            cmbCategory.Enabled = true;
            cmbSupplier.Enabled = true;
            
        }
        void trangthai_an()
        {
            textBox2.ReadOnly = true;
            textBox1.ReadOnly = true;
            cmbSupplier.Enabled = false;
            cmbCategory.Enabled = false;
            btnSave.Enabled = false;
        }
        void xoarong()
        {
            textBox1.Clear();
            textBox2.Clear();
            cmbCategory.SelectedIndex = -1;
            cmbSupplier.SelectedIndex = -1;
        }
        int proID=0;
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow dg = dataGridView1.SelectedRows[0];
            textBox1.Text = dg.Cells["ProductName"].Value.ToString();
            textBox2.Text = dg.Cells["unitprice"].Value.ToString();
            cmbCategory.Text = dg.Cells["CategoryName"].Value.ToString();
            cmbSupplier.Text = dg.Cells["CompanyName"].Value.ToString();
            proID = int.Parse(dg.Cells["ProductID"].Value.ToString());
        }
        int kiemtra;
        private void btnADD_Click(object sender, EventArgs e)
        {
            trangthai_hien();
            xoarong();
            kiemtra = 1;
            btnEDIT.Enabled = false;
            btnADD.Enabled = false;
            btnDEL.Enabled = false;
            btnSave.Enabled = true;
        }

        private void btnEDIT_Click(object sender, EventArgs e)
        {
            if (proID==0)
                MessageBox.Show("May Phai Chon San Pham Da","",MessageBoxButtons.OK,MessageBoxIcon.Error);
            else
            {
                trangthai_hien();
                kiemtra = 0;
                btnEDIT.Enabled = false;
                btnADD.Enabled = false;
                btnDEL.Enabled = false;
                btnSave.Enabled = true;
            }
        }

        private void btnDEL_Click(object sender, EventArgs e)
        {
            try
            {
                if (proID!= 0 && MessageBox.Show("Ban Co Chac?", "Xoa Produc", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    pro.ProductID = proID;
                    pro.Delete(pro);
                    LoadData();
                    xoarong();
                }
            }
            catch (Exception a)
            {
                MessageBox.Show("ERROR:"+a);
            }
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                pro.ProductName = textBox1.Text;
                pro.UnitPrice = double.Parse(textBox2.Text);
                pro.SupplierID = int.Parse(cmbSupplier.SelectedValue.ToString());
                pro.CategoryID = int.Parse(cmbCategory.SelectedValue.ToString());
                pro.ProductID = proID;
                if (kiemtra == 1)
                    pro.Insert(pro);
                else
                    pro.Update(pro);
                MessageBox.Show(">>>>>>>>>>>>OK<<<<<<<<<<");
                xoarong();
                LoadData();
                btnSave.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex);
            }
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            trangthai_an();
            xoarong();
            proID = 0;
            btnEDIT.Enabled = true;
            btnADD.Enabled = true;
            btnDEL.Enabled = true;
            btnSave.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }
    }
}
